package view;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.UIManager;

import panels.SPanel;

import model.App;
import model.PackTableModel;

public class MainFrame extends JFrame {
	private static final long serialVersionUID = -5093064061027429275L;
	private SPanel mainPanel = new SPanel();
	private int width = 600;
	private int height = 550;
	// 设备选择
	private JComboBox<String> devicesField = new JComboBox<String>();
	private JLabel devicesLabel = new JLabel("监听网卡选择");
	private SPanel devicesPanel = new SPanel();
	// 工具栏
	private SPanel toolsPanel = new SPanel();
	private JButton fun1 = new JButton();
	private JButton fun2 = new JButton("开始");
	private JButton fun3 = new JButton("结束");
	private JButton fun4 = new JButton("流量监测");
	private JButton fun5 = new JButton();
	// 信息筛选
	private SPanel filtersPanel = new SPanel();
	private SPanel filtersLine1 = new SPanel();
	private SPanel filtersLine2 = new SPanel();
	private SPanel filtersLine3 = new SPanel();
	private JLabel filter11 = new JLabel("正则筛选");
	private JLabel filter21 = new JLabel("常用协议");
	private JLabel filter31 = new JLabel("源地址");
	private JLabel filter41 = new JLabel("目的地址");
	private JLabel filter51 = new JLabel("截获数量");
	private JLabel filter61 = new JLabel("数据包关键词");
	private JTextField filter12 = new JTextField();
	private JTextField filter22 = new JTextField();
	private JTextField filter32 = new JTextField();
	private JTextField filter42 = new JTextField();
	private JTextField filter52 = new JTextField();
	private JTextField filter62 = new JTextField();
	// 数据报
	private SPanel packetsPanel = new SPanel();
	private JTable packetsTable = new JTable();
	private PackTableModel packetsModel = new PackTableModel();

	public MainFrame() {
		initUI();
		initListener();
		setTitle("SirusSniffer");
		setSize(width, height);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setLocationRelativeTo(null);
		setVisible(true);
	}

	private void initUI() {
		// 加入总界面
		// devicesPanel.setBackground(Color.gray);
		// toolsPanel.setBackground(Color.green);
		// filtersPanel.setBackground(Color.orange);
		// packetsPanel.setBackground(Color.blue);
		mainPanel.add(devicesPanel);
		mainPanel.add(toolsPanel);
		mainPanel.add(packetsPanel);
		mainPanel.add(filtersPanel);
		mainPanel.setSize(width - 15, height);
		mainPanel.setCss(10, (new int[] { 15, 10, 48, 22 }));
		add(mainPanel);
		// 设备选择
		devicesField = App.setComboBox();
		devicesPanel.add(devicesLabel);
		devicesPanel.add(devicesField);
		devicesPanel.setCss(5, (new int[] { 35, 60 }));
		// 工具栏
		fun3.setEnabled(false);
		fun4.setEnabled(false);
		fun5.setEnabled(false);
		toolsPanel.add(fun1);
		toolsPanel.add(fun2);
		toolsPanel.add(fun3);
		toolsPanel.add(fun4);
//		toolsPanel.add(fun5);
		toolsPanel.setStyle(SPanel.VERTICAL);
		toolsPanel.setCss(0, (new int[] { 2, 10, 10, 10, 10 }));
		// 信息筛选
		filtersPanel.add(filtersLine1);
		filtersPanel.add(filtersLine2);
		filtersPanel.add(filtersLine3);

		filtersLine1.add(filter11);
		filtersLine1.add(filter12);
		filtersLine1.add((new SPanel()));
		filtersLine1.add(filter21);
		filtersLine1.add(filter22);

		filtersLine2.add(filter31);
		filtersLine2.add(filter32);
		filtersLine2.add((new SPanel()));
		filtersLine2.add(filter41);
		filtersLine2.add(filter42);

		filtersLine3.add(filter51);
		filtersLine3.add(filter52);
		filtersLine3.add((new SPanel()));
		filtersLine3.add(filter61);
		filtersLine3.add(filter62);

		filtersPanel.setCss(10, (new int[] { 35, 35, 36 }));
		filtersLine1.setStyle(SPanel.VERTICAL);
		filtersLine2.setStyle(SPanel.VERTICAL);
		filtersLine3.setStyle(SPanel.VERTICAL);
		filtersLine1.setCss(0, (new int[] { 15, 30, 5 }));
		filtersLine2.setCss(0, (new int[] { 15, 30, 5 }));
		filtersLine3.setCss(0, (new int[] { 15, 30, 5 }));
		// 数据报
		App.setTable(packetsModel);
		packetsTable.setModel(packetsModel);
		packetsTable.getColumnModel().getColumn(0).setPreferredWidth(10);
		packetsTable.getColumnModel().getColumn(1).setPreferredWidth(10);
		packetsTable.getColumnModel().getColumn(2).setPreferredWidth(80);
		packetsTable.getColumnModel().getColumn(3).setPreferredWidth(80);
		packetsTable.getColumnModel().getColumn(4).setPreferredWidth(200);
		JScrollPane tablePanel = new JScrollPane(packetsTable);
		packetsPanel.add(tablePanel);
		packetsPanel.setCss(0, (new int[] { 100 }));
	}

	private void initListener() {
		// 开始
		fun2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					App.setFilterRes(filter32.getText());
					App.setFilterDes(filter42.getText());
					App.setFilterNum(filter52.getText());
					App.setFilterPro(filter22.getText());
					// App.setFilterPro("tcp");
					App.setFilterReg(filter12.getText());
					App.startCapture("");
				} catch (IOException e1) {
					e1.printStackTrace();
				}
				fun2.setEnabled(false);
				fun3.setEnabled(true);
				fun4.setEnabled(false);
			}
		});
		// 结束
		fun3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				App.closeCapture();
				fun2.setEnabled(true);
				fun3.setEnabled(false);
				fun4.setEnabled(true);
			}
		});
		// 流量
		fun4.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				new SummaryFrame();
			}
		});
		// 双击查看数据包
		packetsTable.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if (e.getClickCount() == 2) {
					int row = ((JTable) e.getSource()).rowAtPoint(e.getPoint()); // 获得行位置
					new PacketFrame(App.packetList.get(row));
				}
			}
		});
	}

	public static void main(String[] args) throws Exception {
		UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
		new MainFrame();
	}

	// private static ImageIcon getIcon(String path) {
	// return new ImageIcon(MainFrame.class.getResource("/res/" + path));
	// }
}
